package com.jackrain.nea.wms.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.wms.api.BaseWmsDelCmd;
import com.jackrain.nea.wms.mapper.WmsSqlMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "wms")
public class BaseWmsDelCmdImpl extends CommandAdapter implements BaseWmsDelCmd {

    @Autowired
    private WmsSqlMapper wmsSqlMapper;

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        if (null == param || param.size() == 0 || param.getJSONArray("data").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }
        log.info("BaseWmsDelCmdImpl--param-->{}",param);

        JSONArray jsonArray = param.getJSONArray("data");
        String startTime = TypeUtils.castToString(System.currentTimeMillis());
        try {

            for (int i = 0; i < jsonArray.size(); i++) {

                JSONObject fixcolum = jsonArray.getJSONObject(i);
                JSONObject mainJo = fixcolum.getJSONObject("mainData");
                String bill_no = mainJo.getString("bill_no");
                //更新主表
                JSONObject whereKeys = new JSONObject();
                whereKeys.put("bill_no", bill_no);
                JSONObject setKeys = new JSONObject();
                setKeys.put("bill_no", bill_no + startTime);
                wmsSqlMapper.update("WMS_T_OUT_NOTICES", setKeys, whereKeys);
                //更新子表
                wmsSqlMapper.update("WMS_T_OUT_NOTICES_ITEM", setKeys, whereKeys);
            }

        } catch (Exception e) {
            throw new NDSException("更新失败" + e.getMessage());
        }
        vh.put("message", "更新成功！");
        return vh;
    }
}
